<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class InternalVehicle extends Model
{
    protected $table = 'internal_vehicle';

    protected $primaryKey = 'print_code';

    protected $keyType = 'string';

    protected $fillable = [
    	'print_code',
    	'ic_id',
        'order_systemnumber',
    	'vehicle_number',
    	'driver_name',
    	'driver_idcard',
    	'driver_mobile_phone',
    	'tare',
    	'update_datetime',
    	'operator',
        'is_void',
        'empty_type',
        'is_loading',
        'purchase_weight',
        'discharge_weight',
        'pick_weight',
        'is_return',
        'is_go',
        'exception',
    	'remarks'
    ];

    protected $appends = [
        'is_void_desc',
        'empty_type_desc',
        'is_loading_desc',
        'relative_weight',
        'is_return_desc',
        'is_go_desc'
    ];

    public $timestamps = false;

    public function getRelativeWeightAttribute(){
        if(array_key_exists('purchase_weight', $this->attributes)){
            if(array_key_exists('discharge_weight', $this->attributes)){
                return $this->attributes['relative_weight'] = $this->attributes['purchase_weight']-$this->attributes['discharge_weight'];
            }
        }
    }

    const IS_GO_ZERO = '0';

    const IS_GO_ONE = '1';

    public static $IS_GO = [
        self::IS_GO_ZERO => '不允许出门',
        self::IS_GO_ONE => '允许出门'
    ];

    public function getIsGoDescAttribute(){
        if(array_key_exists('is_go', $this->attributes) && isset($this->attributes['is_go'])){
            return $this->attributes['is_go_desc'] = self::$IS_GO[$this->attributes['is_go']];
        }
    }

    const IS_RETURN_ZERO = '0';

    const IS_RETURN_ONE = '1';

    public static $IS_RETURN = [
        self::IS_RETURN_ZERO => '否',
        self::IS_RETURN_ONE => '是'
    ];

    public function getIsReturnDescAttribute(){
        if(array_key_exists('is_return', $this->attributes) && isset($this->attributes['is_return'])){
            return $this->attributes['is_return_desc'] = self::$IS_RETURN[$this->attributes['is_return']];
        }
    }

    const IS_LOADING_ZERO = '0';

    const IS_LOADING_ONE = '1';

    public static $IS_LOADING = [
        self::IS_LOADING_ZERO => '未装货',
        self::IS_LOADING_ONE => '已装货'
    ];

    public function getIsLoadingDescAttribute(){
        if(array_key_exists('is_loading', $this->attributes) && $this->attributes['is_loading'] != null){
            return $this->attributes['is_loading_desc'] = self::$IS_LOADING[$this->attributes['is_loading']];
        }
    }

    const IS_VOID_ZERO = '0';

    const IS_VOID_ONE = '1';

    public static $IS_VOID = [
        self::IS_VOID_ZERO => '否',
        self::IS_VOID_ONE => '是'
    ];

    public function getIsVoidDescAttribute(){
        if(array_key_exists('is_void', $this->attributes) && isset($this->attributes['is_void'])){
            return $this->attributes['is_void_desc'] = self::$IS_VOID[$this->attributes['is_void']];
        }
    }

    const EMPTY_TYPE_ZERO = '0';

    const EMPTY_TYPE_ONE = '1';

    const EMPTY_TYPE_TWO = '2';

    public static $EMPTY_TYPE = [
        self::EMPTY_TYPE_ZERO => '装货',
        self::EMPTY_TYPE_ONE => '卸货',
        self::EMPTY_TYPE_TWO => '无操作数据'
    ];

    public function getEmptyTypeDescAttribute(){
        if(array_key_exists('empty_type', $this->attributes) && $this->attributes['empty_type'] != null){
            return $this->attributes['empty_type_desc'] = self::$EMPTY_TYPE[$this->attributes['empty_type']];
        }
    }

    public function weightdata(){
        return $this->hasOne('\App\Models\Weighdata','order_systemnumber','order_systemnumber');
    }
}
